# _*_ coding: utf-8 _*_
"""
--------------------------------------
    File Name:    jwt_auth
    Description:
    Author:       lhw
    Date:        5/28/20
--------------------------------------
    Change Activity:

"""
__auther__ = 'lhw'

from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication

from Common.jwt_auth import parse_payload


class JwtAuthorizationAuthentication(BaseAuthentication):
    """
    请求头的方式来校验token
    """

    def authenticate(self, request):
        token = request.META.get("HTTP_X_ACCESS_TOKEN", '')
        if not token:
            raise exceptions.AuthenticationFailed({'error': '未获取到X_ACCESS_TOKEN请求头', 'status': False})
        result = parse_payload(token)
        if not result['status']:
            raise exceptions.AuthenticationFailed(result)

        return (result, token)